import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueCookies from 'vue-cookies'

// 将px转rem文件
import 'lib-flexible/flexible'

import {
  NavBar,
  Button,
  Field,
  Popup,
  Tabbar,
  TabbarItem,
  Icon,
  Search,
  Swipe,
  SwipeItem,
  Lazyload,
  Toast,
  GoodsAction,
  GoodsActionIcon,
  GoodsActionButton,
  Stepper,
  SwipeCell,
  Card,
  Checkbox,
  CheckboxGroup,
  SubmitBar,
  List,
  Empty,
  AddressList,
  AddressEdit,
  Uploader,
  Dialog,
  Tab,
  Tabs,
  Sidebar,
  SidebarItem

} from 'vant';

Vue
  .use(NavBar)
  .use(Button)
  .use(Field)
  .use(Popup)
  .use(Tabbar)
  .use(TabbarItem)
  .use(Icon)
  .use(Search)
  .use(Swipe)
  .use(SwipeItem)
  .use(Lazyload)
  .use(Toast)
  .use(GoodsAction)
  .use(GoodsActionIcon)
  .use(GoodsActionButton)
  .use(Stepper)
  .use(SwipeCell)
  .use(Card)
  .use(Checkbox)
  .use(CheckboxGroup)
  .use(SubmitBar)
  .use(List)
  .use(Empty)
  .use(AddressList)
  .use(AddressEdit)
  .use(Uploader)
  .use(Dialog)
  .use(Tab)
  .use(Tabs)
  .use(Sidebar)
  .use(SidebarItem)


Vue.config.productionTip = false

Vue.use(VueAxios, axios)
Vue.use(VueCookies)

// 设置请求路径和appkey 放置在原型属性中
Vue.prototype.baseUrl = 'http://www.kangliuyong.com:10002'
Vue.prototype.appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0xB1iV06BeNA='



// 拦截器  请求之前拦截处理数据
axios.interceptors.request.use(config => {
  // console.log(config);
  // 如果是POST请求时，需要拦截处理参数
  if (config.method === 'post') {
    // 整理成post参数
    let paramsStr = '';
    for (let key in config.data) {
      paramsStr += `${key}=${config.data[key]}&`;
    }
    // 重新将post赋值给congfig.data
    config.data = paramsStr.slice(0, -1);
    // console.log(config.data);
  }

  return config;

})

// 定义全局时间过滤器
Vue.filter('formatDate', (data, format) => {
  //data: 需要进行格式化的数据
  //format: 格式

  let date = new Date(data);

  //获取年份
  let year = date.getFullYear().toString();

  if (/(y+)/.test(format)) {
    let yearContent = RegExp.$1;
    format = format.replace(yearContent, year.slice(year.length - yearContent.length));
  }

  //替换月日时分秒
  let dateObject = {
    M: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    m: date.getMinutes(),
    s: date.getSeconds()
  };

  for (let key in dateObject) {
    //生成动态正则表达式
    let reg = new RegExp(`(${key}+)`);
    if (reg.test(format)) {
      let content = RegExp.$1;
      format = format.replace(content, dateObject[key] >= 10 ? dateObject[key] : content.length === 2 ? '0' + dateObject[key] : dateObject[key]);
    }
  }

  return format;
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
